package com.xianniu.ltc.service.impl;

import cn.devezhao.persist4j.engine.ID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rebuild.core.UserContextHolder;
import com.rebuild.core.metadata.EntityHelper;
import com.xianniu.ltc.common.Constant;
import com.xianniu.ltc.mapper.TGonghaikehuMapper;
import com.xianniu.ltc.mapper.TSihaikehuMapper;
import com.xianniu.ltc.mapper.TXnAccountMapper;
import com.xianniu.ltc.mapper.TXnAccountTeamMapper;
import com.xianniu.ltc.model.entity.TGonghaikehuDTO;
import com.xianniu.ltc.model.entity.TSihaikehuDTO;
import com.xianniu.ltc.model.entity.TXnAccountDTO;
import com.xianniu.ltc.model.entity.TXnAccountTeamDTO;
import com.xianniu.ltc.model.po.AddXnAccountTeamPO;
import com.xianniu.ltc.model.po.QueryAccountTeamAdminPO;
import com.xianniu.ltc.model.po.TransferAccountTeamPO;
import com.xianniu.ltc.model.po.UpdateAccountTeamPO;
import com.xianniu.ltc.model.vo.XnAccountTeamVO;
import com.xianniu.ltc.service.XnAccountTeamService;
import com.xxl.sso.core.entity.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author WangRuiJie
 * @Title: XnAccountTeamServiceImpl
 * @Package com.xianniu.ltc.service.impl
 * @Description:
 * @date 2021/6/172:57 下午
 */
@Slf4j
@Service
public class XnAccountTeamServiceImpl implements XnAccountTeamService {

    @Resource
    private TXnAccountTeamMapper accountTeamMapper;

    @Resource
    private TXnAccountMapper accountMapper;
    @Resource
    private TSihaikehuMapper sihaikehuMapper;
    @Resource
    private TGonghaikehuMapper gonghaikehuMapper;

    @Override
    public IPage<XnAccountTeamVO> selectAccountTeamForAdmin(QueryAccountTeamAdminPO po) {
        Page<TXnAccountTeamDTO> page = new Page<>(po.getPageNum(), po.getPageSize());
        IPage<XnAccountTeamVO> dtoList = accountTeamMapper.selectPageList(page, po, UserContextHolder.getTenant());
        return dtoList;
    }

    @Override
    public ReturnT addAccountTeamForAdmin(AddXnAccountTeamPO po) {
        // 查询客户组名称是否已存在
        QueryWrapper<TXnAccountTeamDTO> wrapper = new QueryWrapper<>();
        wrapper.eq("team_name", po.getTeamName());
        TXnAccountTeamDTO dto = accountTeamMapper.selectOne(wrapper);
        if (Objects.nonNull(dto)){
            return new ReturnT(400, "客户组名称已存在");
        }
        // 查询客户组名称是否已存在
        ID userId= ID.newId(EntityHelper.XnAccountTeam);
        TXnAccountTeamDTO teamDTO = new TXnAccountTeamDTO();
        BeanUtils.copyProperties(po, teamDTO);
        teamDTO.setTeamId(String.valueOf(userId));
        teamDTO.setModifiedBy(po.getCreatedBy());
        int result =  accountTeamMapper.insert(teamDTO);
        return result>0? new ReturnT(200,"操作成功"):new ReturnT(400, "操作失败");
    }

    /**
     * 转移分组：查询私海，公海下的客户组所属线索修改为新线索组
     * @param po
     * @return
     */
    @Override
    public ReturnT transferAccountTeamForAdmin(TransferAccountTeamPO po) {
        // 查询客户组名称是否已存在
        QueryWrapper<TXnAccountTeamDTO> wrapperFrom = new QueryWrapper<>();
        wrapperFrom.eq("team_id", po.getFromAccountTeamId());
        TXnAccountTeamDTO dtoFrom = accountTeamMapper.selectOne(wrapperFrom);
        if (Objects.isNull(dtoFrom)){
            return new ReturnT(400, "源客户组不存在");
        }

        // 查询客户组名称是否已存在
        QueryWrapper<TXnAccountTeamDTO> wrapperTo = new QueryWrapper<>();
        wrapperTo.eq("team_id", po.getToAccountTeamId());
        TXnAccountTeamDTO dtoTo = accountTeamMapper.selectOne(wrapperTo);
        if (Objects.isNull(dtoTo)){
            return new ReturnT(400, "目标客户组不存在");
        }
        // 根据源客户组名称修改私海客户的客户组名称为目标客户组
        accountMapper.transferPAccountTeamForAdmin(dtoFrom.getTeamName(), dtoTo.getTeamName());
        // 根据源客户组名称修改公海客户的客户组名称为目标客户组
        accountMapper.transferCAccountTeamForAdmin(dtoFrom.getTeamName(), dtoTo.getTeamName());
        return new ReturnT(200,"操作成功");
    }

    @Override
    public ReturnT updateAccountTeamForAdmin(UpdateAccountTeamPO po) {
        TXnAccountTeamDTO dto = new TXnAccountTeamDTO();
        BeanUtils.copyProperties(po, dto);
        dto.setModifiedOn(new Date());
        UpdateWrapper<TXnAccountTeamDTO> wrapper = new UpdateWrapper<>();
        wrapper.eq("team_id", po.getMetadata().getId());
        int i = accountTeamMapper.update(dto, wrapper);
        return i>0? new ReturnT(200,"操作成功"):new ReturnT(400, "操作失败");
    }

    /**
     * 后台客户组：通过客户组ID查询客户组是否存在
     * 根据客户组查询是否有私海客户，有则提示不能删除
     * 根据客户组查询是否有公海客户，有则提示不能删除
     * @param teamId
     * @return
     */
    @Override
    public ReturnT deleteAccountTeamForAdmin(String teamId) {
        // 查询客户组名称是否已存在
        QueryWrapper<TXnAccountTeamDTO> wrapperFrom = new QueryWrapper<>();
        wrapperFrom.eq("team_id", teamId);
        TXnAccountTeamDTO dtoFrom = accountTeamMapper.selectOne(wrapperFrom);
        if (Objects.isNull(dtoFrom)){
            return new ReturnT(400, "客户组不存在");
        }

        QueryWrapper<TSihaikehuDTO> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("team_id", dtoFrom.getTeamName());
        // 查询当前客户组是否还有私海客户
        List<TSihaikehuDTO> dtoList1 = sihaikehuMapper.selectList(wrapper1);
        if (!dtoList1.isEmpty()) {
            return new ReturnT(400,"客户组存在数据，不允许删除");
        }
        // 查询当前客户组是否还有公海客户
        QueryWrapper<TGonghaikehuDTO> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("team_id", dtoFrom.getTeamName());
        List<TGonghaikehuDTO> dtoList2 = gonghaikehuMapper.selectList(wrapper2);
        if (!dtoList2.isEmpty()) {
            return new ReturnT(400,"客户组存在数据，不允许删除");
        }
        if(teamId.equals(Constant.DEFAULT_TEAM_NAME) || teamId.equals(Constant.DEFAULT_TEAM_NAME2)){
            return new ReturnT(400,"默认客户组不能删除");
        }
        // 根据ID删除客户组
        int i = accountTeamMapper.deleteById(teamId);
        return i>0? new ReturnT(200,"操作成功"):new ReturnT(400, "操作失败");
    }
}
